
const db = wx.cloud.database()
Page({
  data: {
    phone:"",
    isShow:false,//是否显示左侧按钮
    backgroundColor:'#efefef',//背景颜色
    navTitle:'登录',//标题
    isWhite:true,//是否白色胶囊
    titleColor:'black',//字体颜色
  },
  forget(){
    wx.navigateTo({
      url: '/pages/forget_password/forget_password',
    })
  },
  adminLogin(){
    wx.navigateTo({
      url: '/pages/administrator/administrator',
    })
  },
    // 登陆功能
    create_login(e) {
        let user = e.detail.value
        console.log('user', user)
        if (!user.phone) {
            wx.showToast({
                icon: 'none',
                title: '请填写手机',
            })
        } else if (!user.password) {
            wx.showToast({
                icon: 'none',
                title: '请填写密码',
            })
        } else {
            db.collection('user').where({
                    phone: user.phone,
                    
                    password: user.password
                })
                
                .get()
                .then(res => {
                    console.log('获取登录结果', res)
                    let users = res.data
                    if (users && users.length > 0) {
                        let user = users[0]
                        wx.setStorageSync('user2', user)
                        console.log('user2', user)
                        console.log('user.flag',user.flag)

                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    } else {
                        wx.showToast({
                            title: '账号密码错误',
                            icon: "error"
                        })
                    }
                })
                .catch(res => {
                    console.log('获取登录结果失败', res)
                })
                // wx.setStorageSync('phone',user.phone ) 
                // console.log("user.phone",user.phone)      
                // wx.setStorageSync('flag',user.flag ) 
                // console.log("user.flag",user.flag)      
                
        }
    },
    // 去注册
    zhuce(res) {
        wx.navigateTo({
            url: '/pages/register/register',
        })
    },
onLoad(options) {

}
})
